local LoadingLayer = class("LoadingLayer",function()
    return cc.Layer:create()
end)


function LoadingLayer.create()
    local layer = LoadingLayer.new()
    layer:init( )
    return layer
end

function LoadingLayer:ctor()
   
end


function LoadingLayer:init()
    self.m_Json = ccs.GUIReader:getInstance():widgetFromJsonFile("ui/launcher/Login.Json")
    local WinSize = cc.Director:getInstance():getWinSize()
    self.m_Json:setScaleX(WinSize.width/self.m_Json:getContentSize().width)
    self.m_Json:setScaleY(WinSize.height/self.m_Json:getContentSize().height)
    self:addChild(self.m_Json)

    self.m_TextFieldAccount = ccui.Helper:seekWidgetByName(self.m_Json,"TextFieldAccount")
    self.m_TextFieldAccount:setFontName(DEFAULT_FONT)
    self.m_TextFieldAccount:setFontSize(18)
    self.m_TextFieldAccount:setPlaceHolder(LocalizedString("login_account_tips"))
    self.m_TextFieldAccount:setTouchEnabled(true)
    self.m_TextFieldAccount:setText("hgj002")
 
    self.m_TextFieldPassword = ccui.Helper:seekWidgetByName(self.m_Json,"TextFieldPassword")
    self.m_TextFieldPassword:setPlaceHolder(LocalizedString("login_password_tips"))
    self.m_TextFieldPassword:setFontName(DEFAULT_FONT)
    self.m_TextFieldPassword:setFontSize(18)
    self.m_TextFieldPassword:setPasswordEnabled(true)
    self.m_TextFieldPassword:setPasswordStyleText("*")
    self.m_TextFieldPassword:setMaxLengthEnabled(true)
    self.m_TextFieldPassword:setMaxLength(6)
    self.m_TextFieldPassword:addEventListener(handler(self, self.textFieldEvent))
    self.m_TextFieldPassword:setText("201258")
         
  
    self.m_BtnAccount = self.m_Json:getChildByName("BtnAccount")
    self.m_BtnAccount:addTouchEventListener(function(sender,eventType)self:onMenutClick(sender,eventType) end)
  
    self.m_BtnGuest = self.m_Json:getChildByName("BtnGuest")
    self.m_BtnGuest:addTouchEventListener(function(sender,eventType)self:onMenutClick(sender,eventType) end)
    
    self.m_BtnRegister = self.m_Json:getChildByName("BtnReguster")
    self.m_BtnRegister:addTouchEventListener(function(sender,eventType)self:onMenutClick(sender,eventType) end)
    
    addEventListenerCustom(ds.Account.evtConnectResponse, handler(self, self.onConnectResponse), self)
    addEventListenerCustom(ds.Account.evtMainHallLogin, handler(self, self.onMainHallLogin), self)
  
  
end

function LoadingLayer:onConnectResponse( event )
    print("==LoadingLayer:onConnectResponse========")
    local wConnectID = event.data.wConnectID
    local wState = event.data.wState
 
    print("==LoadingLayer:onConnectResponse===wConnectID="..wConnectID.."===wState="..wState)
    
    if( wState ~= 1  ) then
      require("widget/PromptBox").show(LocalizedString("Waitingbox_connect_lose"))
    end
  
end

function LoadingLayer:sendLogin()
    -- 开始登录
    local packout = dv.LuaPacket:new(300)
    local Account = self.m_TextFieldAccount:getStringValue()
    local Password = self.m_TextFieldPassword:getStringValue()

    local targetPlatform = cc.Application:getInstance():getTargetPlatform()

    local APPID = 103
    if(targetPlatform == cc.PLATFORM_OS_ANDROID) then
        APPID = 101
    elseif (CC_TARGET_PLATFORM==cc.PLATFORM_OS_MAC and CC_TARGET_PLATFORM==cc.PLATFORM_OS_IPHONE and CC_TARGET_PLATFORM==cc.PLATFORM_OS_IPAD ) then
        APPID = 102
    end

    local bodydata = Struct:getInstance():getBodyData('CMD_CS_UserLoginHall') 
    Struct:getInstance():setValue(bodydata, "iVersion", 100000)
    Struct:getInstance():setValue(bodydata, "iAppId", 102)
    Struct:getInstance():setValue(bodydata, "szName", Account)
    Struct:getInstance():setValue(bodydata, "szPwd", MD5String(Password))
    Struct:getInstance():setValue(bodydata, "szIp", "127.0.0.1")
    Struct:getInstance():setValue(bodydata, "iTrenchId", 0)
    Struct:getInstance():setValue(bodydata, "iPlatformNo", 1)
    Struct:getInstance():setValue(bodydata, "szMachineCode", "5644613231")
    Struct:getInstance():writeBody(packout, bodydata)


    print("====MAIN_HALL_MSG_LOGIN========"..Cmd.MAIN_HALL_MSG_LOGIN.."===SUB_CS_USER_LOGIN_HALL==="..Cmd.SUB_CS_USER_LOGIN_HALL)

    SendData(Network.SERVER_LOGIN,Cmd.MAIN_HALL_MSG_LOGIN,Cmd.SUB_CS_USER_LOGIN_HALL,packout)
end


function LoadingLayer:onMainHallLogin( event )
    local wSubCmdID = event.data.wSubCmdID 
    local data = event.data.data 
 
    if(wSubCmdID == Cmd.SUB_SC_USER_LOGIN_HALL_SUC ) then  -- 用户登录成功
   
    elseif(wSubCmdID == Cmd.SUB_SC_USER_LOGIN_HALL_FINISH ) then -- 用户登录完成 
        
    elseif(wSubCmdID == Cmd.SUB_SC_GAME_HALL_LIST_SUCC ) then  -- 获取大厅游戏信息 成功
  
    elseif(wSubCmdID == Cmd.SUB_SC_GAME_HALL_LIST_FINISH ) then -- 获取大厅游戏信息完成
        require "view/Launcher/LauncherLayer".show()
    elseif(wSubCmdID == Cmd.SUB_SC_USER_LOGIN_HALL_FAIL ) then  -- 用户登录失败
        require("widget/PromptBox").show(data.szResText)
    end

end

function LoadingLayer:textFieldEvent(sender, eventType)
    if eventType == ccui.TextFiledEventType.attach_with_ime then
  
    elseif eventType == ccui.TextFiledEventType.detach_with_ime then
   
    elseif eventType == ccui.TextFiledEventType.insert_text then
 
    elseif eventType == ccui.TextFiledEventType.delete_backward then
    
    end
end

function LoadingLayer:onMenutClick(sender,eventType)
    if eventType == ccui.TouchEventType.ended then
        if sender == self.m_BtnAccount then     
            print "=========SetLayer:onMenutClick m_BtnAccount======================="
            self:sendLogin()
        elseif sender == self.m_BtnGuest then     
            print "=========SetLayer:onMenutClick m_BtnGuest======================="
            self:sendLogin()
        elseif sender == self.m_BtnRegister then     
            print "=========SetLayer:onMenutClick m_BtnRegister======================="
            require "view/Register/RegisterLayer".show()
        end
    end
end
 
return LoadingLayer